// import $store from '@/store/index.js';
import config from '@/config'
console.log(process.env.NODE_ENV,'环境');
export default {
	// 全局配置
	common: {
		baseUrl: config[process.env.NODE_ENV === 'development'?'dev':'prod'].apiUrl,
		header: {
			'Content-Type': 'application/json;charset=UTF-8',
		},
		data: {},
		method: 'GET',
		dataType: 'json',
		token: true
	},
	// 请求 返回promise
	request(options = {}) {
		// 组织参数
		options.url = this.common.baseUrl + options.url
		options.header = options.header || this.common.header
		options.data = options.data || this.common.data
		options.method = options.method || this.common.method
		options.dataType = options.dataType || this.common.dataType
		options.token = options.token === false ? false : this.common.token

		// 请求
		return new Promise((res, rej) => {
			// 请求之前验证...
			// token验证
			if (options.token) {
				// options.header.Authorization = "";
				// let token = uni.getStorageSync('token')
				let token = 'e83371d5-cc65-4d90-8162-2b21a4d63e69'
				if (token) {
					// 往header头中添加token
					options.header.Authorization = "Bearer " + token
				} else {
					options.header.Authorization = "";
				}
			} else {
				options.header.Authorization = "";
			}

			uni.showToast({
				title: '加载中',
				icon: 'loading',
				duration: 10000
			});

			// 请求中...
			uni.request({
				...options,
				success: (result) => {
					uni.hideToast();
					// 返回原始数据
					if (options.native) {
						return res(result)
					}

					if (result.data.code == 401) {
						//跳转登录
						uni.showToast({
							title: '认证失败，无法访问系统资源',
							icon: 'none'
						});

						let time = setTimeout(() => {
							clearTimeout(time);
							uni.navigateTo({
								url: '/pages/login/login',
							});
						}, 800);
						return;
					}
					// 服务端失败
					if (result.statusCode !== 200) {
						if (options.toast !== false) {
							uni.showToast({
								title: result.data.data || '服务端失败',
								icon: 'none'
							});
						}
						// token不合法，直接退出登录
						if (result.data.data === 'Token 令牌不合法!') {
							// 退出登录操作
							// $store.dispatch('logout')
						}
						return rej(result.data)
					}
					// 其他验证...

					if (result.data.code !== 200) {
						uni.showModal({
							title: '提示',
							content: result.data.msg || '服务端失败',
							showCancel: false,
							success: function (res) {
								if (res.confirm) {
									// uni.navigateBack({
									// 	delta:1
									// })
								}
							}
						});
						// uni.showToast({
						// 	title: result.data.msg || '服务端失败',
						// 	icon: 'none'
						// });
					}

					// 成功
					let data = result.data
					res(data)
				},
				fail: (error) => {
					uni.hideToast();
					uni.showToast({
						title: error.errMsg || '请求失败',
						icon: 'none'
					});
					return rej(error)
				}
			});
		})
	},
	// get请求
	get(url, options = {}) {
		options.url = url
		options.data = {}
		options.method = 'GET'
		return this.request(options)
	},
	// post请求
	post(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	// delete请求
	del(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	},
	// 上传文件
	upload(url, data, onProgress = false) {
		return new Promise((result, reject) => {
			// 上传
			let token = uni.getStorageSync('token')
			if (!token) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				});
				// token不存在时跳转
				return uni.reLaunch({
					url: '/pages/login/login',
				});
			}

			const uploadTask = uni.uploadFile({
				url: this.common.baseUrl + url,
				filePath: data.filePath,
				name: data.name || "files",
				header: {
					token
				},
				formData: data.formData || {},
				success: (res) => {
					if (res.statusCode !== 200) {
						result(false)
						return uni.showToast({
							title: '上传失败',
							icon: 'none'
						});
					}
					let message = JSON.parse(res.data)
					result(message.data);
				},
				fail: (err) => {
					reject(err)
				}
			})

			uploadTask.onProgressUpdate((res) => {
				if (typeof onProgress === 'function') {
					onProgress(res.progress)
				}
			});

		})
	}
}
